import { NextResponse } from 'next/server';
import { getServerSession } from 'next-auth/next';
import { authOptions } from '@/app/api/auth/[...nextauth]/route';
import { api } from '@/lib/apiService';

export async function GET(request) {
  try {
    const session = await getServerSession(authOptions);
    if (!session?.user?.email) {
      return NextResponse.json({ error: '未授权访问' }, { status: 401 });
    }

    const { searchParams } = new URL(request.url);
    const title = searchParams.get('title');
    const version = searchParams.get('version');

    if (!title || !version) {
      return NextResponse.json({ error: '标题和版本号不能为空' }, { status: 400 });
    }

    // 使用统一的API服务调用后端
    const response = await api.auth.get(`prompts/check-duplicate?title=${encodeURIComponent(title)}&version=${encodeURIComponent(version)}`);
    
    return NextResponse.json(response);
  } catch (error) {
    console.error('Error checking duplicate prompt:', error);
    return NextResponse.json({ error: '检查重复失败' }, { status: 500 });
  }
}
